<!-- DOMContentLoaded事件 和 onload 事件 的触发时机 -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./jquery.min.js"></script>
  </head>
  <body>
    <div>DOMContentLoaded事件 和 onload 事件 的触发时机</div>

    <img src="https://gitee.com/static/images/logo-black.svg?t=158106664" alt="" />

    <script>
      // 触发时机比 onload 早
      $(function () {
        console.log('jQuery 的入口函数触发啦111~')
      })

      $(function () {
        console.log('jQuery 的入口函数触发啦222~')
      })

      $(function () {
        console.log('jQuery 的入口函数触发啦333~')
      })

      // 在页面所有资源加载完成后触发，触发时机比 DOMContentLoaded 要晚
      window.onload = function () {
        console.log('window.onload 触发了111')
      }

      window.onload = function () {
        console.log('window.onload 触发了222')
      }

      window.addEventListener('load', function () {
        console.log('window.onload 触发了333')
      })
      window.addEventListener('load', function () {
        console.log('window.onload 触发了444')
      })

      // DOMContentLoaded 仅需等待 DOM 结构加载完成后触发，触发时机比 onload 早
      document.addEventListener('DOMContentLoaded', function () {
        console.log('DOMContentLoaded 触发了111')
      })

      document.addEventListener('DOMContentLoaded', function () {
        console.log('DOMContentLoaded 触发了222')
      })

      document.addEventListener('DOMContentLoaded', function () {
        console.log('DOMContentLoaded 触发了333')
      })
    </script>
  </body>
</html>
